package com.zx.controller;

import com.zx.entity.User;
import com.zx.service.EnterInte;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;

/**
 * @author 程仟磊
 * @date 2021/7/22
 */
@SessionAttributes({"user","uid"})
@Controller
public class Enter {
    private static final String REDIRECT="redirect:";
    @Resource
    private EnterInte enterInte;
    @RequestMapping("/login")
    @ResponseBody
    //登录功能
    public String login(ModelMap modelMap, String uaccount, String upsw){
        User user = enterInte.findLogin(uaccount, upsw);
        if (user==null){
            return "error";
        }else {
            modelMap.addAttribute("user",user.getUsname());
            modelMap.addAttribute("uid",user.getUsid());
           return "true";
        }
    }
    //注册
        @RequestMapping(value = "/register")
    public String register(String uname,String uaccount,String upsw,String uphone){
        int register = enterInte.addRegister(uaccount, upsw, uphone, uname);
        if (register==1){
          return "login";
        }else {
            return REDIRECT+"500";
        }
    }
    @ResponseBody
    @RequestMapping("/findAccount")
    public String findAccount(String uaccount){
        int account = enterInte.findAccount(uaccount);
        if (account>0){
            return "false";
        }else {
            return "true";
        }
    }
}
